package com.ruoyi.project.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.Doctorstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.remake.domain.constant.InitConstant;
import com.ruoyi.project.remake.domain.dto.LoginDoctorUser;
import com.ruoyi.project.remake.domain.dto.LoginSysUser;
import com.ruoyi.project.remake.service.RemakeLoginService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TDoctorClass;
import com.ruoyi.project.system.mapper.TDoctorClassMapper;
import com.ruoyi.project.system.service.DoctorUserLoginService;
import com.ruoyi.project.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.TDoctorUserMapper;
import com.ruoyi.project.system.domain.TDoctorUser;
import com.ruoyi.project.system.service.ITDoctorUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 医生管理Service业务层处理
 * 
 * @author 孙山伟
 * @date 2020-12-20
 */
@Service
public class TDoctorUserServiceImpl implements ITDoctorUserService 
{
    @Autowired
    private TDoctorUserMapper tDoctorUserMapper;
    @Autowired
    private TDoctorClassMapper tDoctorClassMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private DoctorUserLoginService doctorUserLoginService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询医生管理
     * 
     * @param id 医生管理ID
     * @return 医生管理
     */
    @Override
    public TDoctorUser selectTDoctorUserById(Long id)
    {
        return tDoctorUserMapper.selectTDoctorUserById(id);
    }

    /**
     * 查询医生管理列表
     * 
     * @param tDoctorUser 医生管理
     * @return 医生管理
     */
    @Override
    public List<TDoctorUser> selectTDoctorUserList(TDoctorUser tDoctorUser)
    {
        return tDoctorUserMapper.selectTDoctorUserList(tDoctorUser);
    }

    /**
     * 新增医生管理
     * 
     * @param tDoctorUser 医生管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTDoctorUser(TDoctorUser tDoctorUser)
    {
        tDoctorUser.setPassword(SecurityUtils.encryptPassword(tDoctorUser.getPassword()));
        tDoctorUser.setCreateTime(DateUtils.getNowDate());
        int id=tDoctorUserMapper.insertTDoctorUser(tDoctorUser);
        String [] ids=tDoctorUser.getClasstype();
        if(ids!=null&&ids.length!=0){
            for(String ss:ids){
                TDoctorClass doctorClass=new TDoctorClass();
                doctorClass.setClassValue(ss);
                doctorClass.setDoctorId(tDoctorUser.getId());
                tDoctorClassMapper.insertTDoctorClass(doctorClass);
            }
        }

        return id;
    }

    /**
     * 修改医生管理
     * 
     * @param tDoctorUser 医生管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTDoctorUser(TDoctorUser tDoctorUser)
    {
        tDoctorUser.setUpdateTime(DateUtils.getNowDate());
        int id=tDoctorUserMapper.updateTDoctorUser(tDoctorUser);
        tDoctorClassMapper.deleteByDoctorId(tDoctorUser.getId());
        String [] ids=tDoctorUser.getClasstype();
        if(ids!=null&&ids.length!=0){
            for(String ss:ids){
                TDoctorClass doctorClass=new TDoctorClass();
                doctorClass.setClassValue(ss);
                doctorClass.setDoctorId(tDoctorUser.getId());
                tDoctorClassMapper.insertTDoctorClass(doctorClass);
            }
        }
        return id;
    }

    /**
     * 批量删除医生管理
     * 
     * @param ids 需要删除的医生管理ID
     * @return 结果
     */
    @Override
    public int deleteTDoctorUserByIds(Long[] ids)
    {
        return tDoctorUserMapper.deleteTDoctorUserByIds(ids);
    }

    /**
     * 删除医生管理信息
     * 
     * @param id 医生管理ID
     * @return 结果
     */
    @Override
    public int deleteTDoctorUserById(Long id)
    {
        return tDoctorUserMapper.deleteTDoctorUserById(id);
    }

    @Override
    public String checkUserNameUnique(String userName) {
        int count = tDoctorUserMapper.checkUserNameUnique(userName);
        if (count > 0)
        {
            return Doctorstants.NOT_UNIQUE;
        }
        return Doctorstants.UNIQUE;
    }

    @Override
    public String checkPhoneUnique(TDoctorUser tDoctorUser) {
        Long userId = StringUtils.isNull(tDoctorUser.getId()) ? -1L : tDoctorUser.getId();
        TDoctorUser info = tDoctorUserMapper.checkPhoneUnique(tDoctorUser.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return Doctorstants.NOT_UNIQUE;
        }
        return Doctorstants.UNIQUE;
    }

    @Override
    public int resetPwd(TDoctorUser tDoctorUser) {
        return tDoctorUserMapper.updateTDoctorUser(tDoctorUser);
    }

    /**
     * 医生在C端登录
     * @param userName
     * @param password
     * @return
     */
    @Override
    public AjaxResult loginPrint(String userName, String password) {
        AjaxResult ajax = AjaxResult.success();
        TDoctorUser tDoctorUser=tDoctorUserMapper.selectUserByUserName(userName);
        if(null==tDoctorUser){
            return  AjaxResult.error("不存在该用户");
        }
        if (!passwordEncoder.matches(password, tDoctorUser.getPassword())) {
            return AjaxResult.error("用户名或密码错误");
        }
        LoginDoctorUser loginUser=new LoginDoctorUser();
        BeanUtils.copyProperties(tDoctorUser, loginUser);
        ajax.put(Constants.TOKEN, doctorUserLoginService.createToken(loginUser));
        ajax.put(Constants.UUID, tDoctorUser.getId());
        ajax.put(Constants.LOGIN_NAME, tDoctorUser.getNickName());
        ajax.put(Constants.URL_SERVER, configService.selectConfigByKey(InitConstant.PRO_IMG_KEY));
        return ajax;
    }


}
